package ollama.ai.three.service.impl;

import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import ollama.ai.three.config.ChatMemoryConfig;
import ollama.ai.three.service.ChatMemoryService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

/**
 * @Auther:Liu
 * @Testname:ChatMemoryServiceImpl
 * @Date:2025/9/23 11:47
 */
@Service
@Slf4j
public class ChatMemoryServiceImpl implements ChatMemoryService {
    @Getter
    private String userName="你是张三，来自于宝鸡文理学院。请以大学生的幽默欢快的语气说话";
    @Resource
    @Lazy
    private ChatClient chatClient;
    @Resource
    @Lazy
    private ChatMemoryConfig chatMemoryConfig;

    @Override
    public Flux<String> getMessage(String role, String question) {
        // 设置新的角色信息
        String s = this.setNewMessage(role);
        log.info("Setting role to: {}", s);

        
        // 获取包含最新角色设置的ChatClient实例
        //ChatClient updatedChatClient = chatMemoryConfig.ClientConfig();
        
        // 使用最新配置的ChatClient实例处理用户问题
        return chatClient.prompt().user(question).stream().content();
    }

    public String setNewMessage(String newMessage){
        userName=newMessage;
        return this.getNewMessage();
    }

    public String getNewMessage(){
        return userName;
    }

}
